package com.yunxi.service.dubboService.impl.account;

import com.github.pagehelper.PageInfo;
import com.yunxi.account.pojo.AccountCreditVO;
import com.yunxi.account.service.AccountCreditDubboService;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.account.AccountCredit;
import com.yunxi.service.localService.account.AccountCreditService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 帐户授信额度配置 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-18
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class AccountCreditDubboServiceImpl implements AccountCreditDubboService {

   @Autowired
   @Qualifier(value = "accountCreditServiceImpl")
   AccountCreditService accountCreditService;


   @Override
   public AccountCreditVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      AccountCredit accountCredit = accountCreditService.getById(id);
      AccountCreditVO accountCreditVO = new AccountCreditVO();
      BeanUtils.copyProperties(accountCredit, accountCreditVO);
      return accountCreditVO;
   }

   @Override
   public void saveOrUpdate(List<AccountCreditVO> accountCreditVOList, LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      for (AccountCreditVO accountCreditVO : accountCreditVOList) {
         if (StringUtils.isEmpty(accountCreditVO.getCreditId())) {
            accountCreditVO.setCreditId(UUID.randomUUID().toString().replaceAll("-", ""));
            accountCreditVO.setCreateBy(loginInfo.getRealName());
         }
         accountCreditVO.setUpdateBy(loginInfo.getRealName());
         AccountCredit accountCredit = new AccountCredit();
         BeanUtils.copyProperties(accountCreditVO, accountCredit);
         accountCreditService.saveOrUpdate(accountCredit);
      }
   }

   @Override
   public void removeById(String id, LoginInfo loginInfo) {
      AccountCredit accountCredit = accountCreditService.getById(id);
      accountCredit.setUpdateBy(loginInfo.getRealName());
      accountCreditService.removeById(id);
   }

   @Override
   public PageInfo<AccountCreditVO> findPageByVO(AccountCreditVO accountCreditVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<AccountCreditVO> pageInfo = accountCreditService.pageByVo(accountCreditVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public List listByVo(AccountCreditVO accountCreditVO) {
      return accountCreditService.listByVo(accountCreditVO);
   }

}
